package com.lzhl.buildjar.dao.impl;

import com.lzhl.buildjar.dao.BaseDao;
import com.lzhl.buildjar.dao.SchemeDao;
import com.lzhl.buildjar.exception.DaoException;
import com.lzhl.buildjar.po.Category;
import com.lzhl.buildjar.po.Scheme;
import com.lzhl.buildjar.util.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 李柱良(@柱子的日月)
 * @date 2014/6/20
 */
public class SchemeDaoImpl extends BaseDao implements SchemeDao {

    @Override
    public Scheme getScheme(long id) {
        Connection conn;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = getConnection();

            StringBuffer sql = new StringBuffer();
            sql.append("select \n");
            sql.append("    FCategoryID, FName, FSeq, \n");
            sql.append("    FClasspath, FOutput, FManifest, FMainclass, FOpNote, FNote, FClassitems \n");
            sql.append("from T_Scheme \n");
            sql.append("where id = ? \n");

            pstmt = conn.prepareStatement(sql.toString());
            pstmt.setLong(1, id);

            rs = pstmt.executeQuery();
            if (rs.next()) {
                Scheme scheme = new Scheme();
                scheme.setId(id);
                Category category = new Category();
                category.setId(rs.getLong("FCategoryID"));
                scheme.setCategory(category);
                scheme.setName(rs.getString("FName"));
                scheme.setSeq(rs.getInt("FSeq"));

                scheme.setClasspath(rs.getString("FClasspath"));
                scheme.setOutput(rs.getString("FOutput"));
                scheme.setManifest(rs.getString("FManifest"));
                scheme.setMainclass(rs.getString("FMainclass"));
                scheme.setOpNote(rs.getInt("FOpNote") == 1);
                scheme.setNote(rs.getString("FNote"));

                String str = rs.getString("FClassitems");
                scheme.setClassitems(StringUtils.toList(str));

                return scheme;
            }
        }
        catch (Exception e) {
            throw new DaoException("方案查询失败", e);
        }
        finally {
            close(pstmt, rs);
        }

        return null;
    }

    @Override
    public List<Scheme> getSchemes(Category category) {
        List<Scheme> schemes = new ArrayList<Scheme>();

        Connection conn;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = getConnection();

            StringBuffer sql = new StringBuffer();
            sql.append("select \n");
            sql.append("    id, FName, FSeq \n");
            sql.append("from T_Scheme \n");
            sql.append("where FCategoryID = ? \n");
            sql.append("order by FName \n");

            pstmt = conn.prepareStatement(sql.toString());
            pstmt.setLong(1, category.getId());

            rs = pstmt.executeQuery();
            while (rs.next()) {
                Scheme scheme = new Scheme();
                scheme.setId(rs.getLong("id"));
                scheme.setCategory(category);
                scheme.setName(rs.getString("FName"));
                scheme.setSeq(rs.getInt("FSeq"));
                schemes.add(scheme);
            }
        }
        catch (Exception e) {
            throw new DaoException("方案查询失败", e);
        }
        finally {
            close(pstmt, rs);
        }

        return schemes;
    }

    @Override
    public Scheme addScheme(Scheme scheme) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = getConnection();

            StringBuffer sql = new StringBuffer();
            sql.append("insert into T_Scheme ( \n");
            sql.append("    FCategoryID, FName, FSeq, \n");
            sql.append("    FClasspath, FOutput, FManifest, FMainclass, FOpNote, FNote, FClassitems \n");
            sql.append(") \n");
            sql.append("values \n");
            sql.append("(?, ?, ?, ?, ?, ?, ?, ?, ?, ?) \n");

            int index = 0;
            pstmt = conn.prepareStatement(sql.toString());
            pstmt.setLong(++index, scheme.getCategory().getId());
            pstmt.setString(++index, scheme.getName());
            pstmt.setInt(++index, scheme.getSeq());

            pstmt.setString(++index, scheme.getClasspath());
            pstmt.setString(++index, scheme.getOutput());
            pstmt.setString(++index, scheme.getManifest());
            pstmt.setString(++index, scheme.getMainclass());
            pstmt.setInt(++index, scheme.isOpNote() ? 1 : 0);
            pstmt.setString(++index, scheme.getNote());
            pstmt.setString(++index, StringUtils.toString(scheme.getClassitems()));

            pstmt.executeUpdate();
            pstmt.close();

            sql.setLength(0);
            sql.append("SELECT LAST_INSERT_ROWID()");

            pstmt = conn.prepareStatement(sql.toString());
            rs = pstmt.executeQuery();

            if(rs.next()) {
                scheme.setId(rs.getLong(1));
            }

            conn.commit();
        }
        catch (Exception e) {
            rollback(conn);
            throw new DaoException("方案添加失败", e);
        }
        finally {
            close(pstmt, rs);
        }

        return scheme;
    }

    @Override
    public Scheme saveScheme(Scheme scheme) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = getConnection();

            StringBuffer sql = new StringBuffer();

            sql.append("update T_Scheme set \n");
            sql.append("    FCategoryID = ?, FName = ?, FSeq = ?, \n");
            sql.append("    FClasspath = ?, FOutput = ?, FManifest = ?, FMainclass = ?, \n");
            sql.append("    FOpNote = ?, FNote = ?, FClassitems = ? \n");
            sql.append("where id = ? \n");

            pstmt = conn.prepareStatement(sql.toString());

            int index = 0;
            pstmt.setLong(++index, scheme.getCategory().getId());
            pstmt.setString(++index, scheme.getName());
            pstmt.setInt(++index, scheme.getSeq());
            pstmt.setString(++index, scheme.getClasspath());
            pstmt.setString(++index, scheme.getOutput());
            pstmt.setString(++index, scheme.getManifest());
            pstmt.setString(++index, scheme.getMainclass());
            pstmt.setInt(++index, scheme.isOpNote() ? 1 : 0);
            pstmt.setString(++index, scheme.getNote());
            pstmt.setString(++index, StringUtils.toString(scheme.getClassitems()));
            pstmt.setLong(++index, scheme.getId());

            pstmt.executeUpdate();

            conn.commit();
        }
        catch (Exception e) {
            rollback(conn);
            throw new DaoException("方案保存失败", e);
        }
        finally {
            close(pstmt);
        }

        return scheme;
    }

    public void deleteScheme(long id) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = getConnection();

            StringBuffer sql = new StringBuffer();

            sql.append("delete from T_Scheme \n");
            sql.append("where id = ? \n");
            pstmt = conn.prepareStatement(sql.toString());
            pstmt.setLong(1, id);
            pstmt.executeUpdate();
            pstmt.close();

            conn.commit();
        }
        catch (Exception e) {
            rollback(conn);
            throw new DaoException("方案删除失败", e);
        }
        finally {
            close(pstmt);
        }
    }

    public boolean isExists(Category category, String name) {
        Connection conn;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = getConnection();

            StringBuffer sql = new StringBuffer();
            sql.append("select 1 from T_Scheme \n");
            sql.append("where FCategoryID = ? and FName = ? \n");

            pstmt = conn.prepareStatement(sql.toString());
            pstmt.setLong(1, category.getId());
            pstmt.setString(2, name);

            rs = pstmt.executeQuery();
            if (rs.next()) {
                return true;
            }
        }
        catch (Exception e) {
            throw new DaoException("方案查询失败", e);
        }
        finally {
            close(pstmt, rs);
        }

        return false;
    }
}
